<?php
/**
 * 生成wm6.5 manila所用的数据格式
 */
class _interface extends __interface {

	private $wcs_day_time_start = 19;	// 晚上开始时间  19:00
	private $wcs_day_time_end = 7;		// 晚上结束时间  7:00
	
	// 白天
	private $wcc_weather = array(
			'01' => '晴天',
			'02' => '以晴为主',
			'03' => '多云见晴',
			'04' => '见断多云',
			'05' => '阳光朦胧',
			'06' => '大部多云',
			'07' => '多云',
			'08' => '多云',
			'11' => '有雾',
			'12' => '阵雨',
			'13' => '大部多云，有阵雨',
			'14' => '多云间阴，有阵雨',
			'15' => '雷阵雨',
			'16' => '大部多云，有雷阵雨',
			'17' => '多云见晴，有雷阵雨',
			'18' => '有雨',
			'19' => '阵雪',
			'20' => '大部多云，有雷阵雪',
			'21' => '多云见晴，有雷阵雪',
			'22' => '雪',
			'23' => '大部多云，有雪',
			'24' => '结冰',
			'25' => '雨夹雪',
			'26' => '冻雨',
			'29' => '雨夹雪',
			'30' => '炎热',
			'31' => '寒冷',
			'32' => '有风',
			'33' => '晴',
			'34' => '大部晴朗',
			'35' => '局部多云',
			'36' => '间断多云',
			'37' => '有霾',
			'38' => '大部多云',
			'39' => '局部多云，有阵雨',
			'40' => '大部多云，有阵雨',
			'41' => '局部多云，有雷阵雨',
			'42' => '大部多云，有雷阵雨',
			'43' => '局部多云，有阵雪',
			'44' => '大部多云，有阵雪',
	);
	private $weathercn_wcc_convert = array(
			'晴' => '01',
			'多云' => '07',
			'晴间多云' => '02',
			'阴' => '06',
			'阵雨' => '12',	
			'雷阵雨' => '15',
			'小雨' => '18',
			'中雨' => '18',
			'大雨' => '18',
			'暴雨' => '18',
			'中到大雨' => '18',
			'大到暴雨' => '18',
			'雾' => '11',
	        '阵雪' => '19',
	        '小雪' => '23',
	        '中雪' => '23',
	        '暴雪' => '22',
	);
	
	private $wcc_kph = array(
			'0' => 1,
			'1' => 5,
			'2' => 11,
			'3' => 19,
			'4' => 28,
			'5' => 38,
			'6' => 49,
			'7' => 51,
			'8' => 74,
			'9' => 88,
			'10' => 102,
			'11' => 117,
			'12' => 133,
			'13' => 149,
			'14' => 166,
			'15' => 183,
			'16' => 201,
			'17' => 220
	);
	private $windDirectionMap = array(
			"南" => "S",
			"北" => "N",
			"东" => "E",
			"西" => "W",
			"东南" => "SE",
			"西南" => "WS",
			"东北" => "NE",
			"西北" => "NW",
			"微" => "None",
			"旋转" => "None",
			"无持续向" => "None"
	);
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see __interface::dispose_data()
	 */
	public function dispose_data() {
		$fx = array();
		$tq1 = array();
		
		$qw1 = array();
		$qw2 = array();
		$cityname = $this->weather_data['city_2']."-".$this->weather_data['city_1'];
		$province = $this->weather_data['city_3'];
		
		$date = date("Y-"). $this->weather_data["update_time"]["month"]. "-". $this->weather_data["update_time"]["day"];
		
		// 获取数据完成，开始生成指定格式数据
		$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
<weather>
<location name=\"{$cityname}\" country=\"{$province}\" />
<localTime>".date("n/j/Y h:i:s A")."</localTime>";
		
		/********************************************* 今天天气数据 *********************************************/
		// 处理风向数据
		$fx = str_replace ( "风", "", $this->weather_data['today']["wind_direction_1"] );
		$wind = $this->windDirectionMap[$fx];
		// 处理风力数据 kph 每小时公里数 mph 每小时英里数
		$mps = str_replace ( array ( "小于", "级"), "", $this->weather_data['today']["wind_power_1"]);
		if ($mps == "微风") $mps = 1;
		if (mb_strpos ( $mps, "-" )) $mps = mb_substr ( $mps, mb_strpos ( $mps, "-" ) + 1 );
		if (! strlen ( $mps )) $mps = 0;
		$kph = isset($this->wcc_kph[$mps]) ? $this->wcc_kph[$mps] : "";
		$mph = intval ( $kph * 1.6093 );
		
		// 处理天气
		$tq1 = $this->weather_data['today']["weather_1"];
		$tq1 = str_replace("，", "转", $tq1);
		if(false!==strpos($tq1, "转")){
			$arr = explode("转", $tq1);
			$tq1 = $arr[0];
		}
		
		// 处理温度
		$qw1 = intval ( $this->weather_data ['today'] ["temperature_1"] );
		$qw2 = intval ( $this->weather_data ['today'] ["temperature_2"] );

		// 处理实时温度
		if(array_key_exists("realtime", $this->weather_data)){
			$qw_now = max($this->weather_data ['realtime'] ['temp'], $this->weather_data ['realtime'] ['tempn']);
		}else{
			$qw_now = $qw1 > $qw2 ? $qw1 : $qw2;
		}
		// 如果实时温度比最高温度高，则实时温度变成最高温度，比最低温度也做此处理
		if ($qw1 > $qw2) { // 1是高温
			if ($qw_now > $qw1)
				$qw1 = $qw_now;
			if ($qw_now < $qw2)
				$qw2 = $qw_now;
		} else { // 2是高温
			if ($qw_now > $qw2)
				$qw2 = $qw_now;
			if ($qw_now < $qw1)
				$qw1 = $qw_now;
		}
		
		// 处理晚上的实时天气
		$tq_now = $tq1;
		if (date ( "H" ) < $this->wcs_day_time_end || date ( "H" ) >= $this->wcs_day_time_start) {		// 晚上
			$tq_now = isset ( $this->weathercn_wcc_convert [$tq_now] ) ? $this->weathercn_wcc_convert [$tq_now] : $this->weathercn_wcc_convert [$tq_now];
		} else {
			$tq_now = $this->weathercn_wcc_convert [$tq_now];
		}

		$xml .= "
<current>
	<temp c=\"{$qw_now}\" f=\"" . ($qw_now * 1.8 + 32) . "\" />
	<condition icon=\"{$tq_now}\">" . $this->weathercn_wcc_convert [$tq1] . "</condition>
</current>
<forecast>";
		if (! $this->wcs_night_icons && (date ( "H" ) < $this->wcs_day_time_end || date ( "H" ) >= $this->wcs_day_time_start)) {
			//$tq1 = $this->weathercn_wcc_convert [$tq1]+32;
			$tq1 = $this->weathercn_wcc_convert [$tq1];
		} else {
			$tq1 = $this->weathercn_wcc_convert [$tq1];
		}
			
		$xml .= "
	<day number=\"0\">
		<name>" . date ( "D", strtotime ( $date ) ) . "</name>
		<date>" . date ( "n/j/Y", strtotime ( $date ) ) . "</date>
		<condition icon=\"" . $tq1 . "\">" . $this->wcc_weather[$tq1] . "</condition>
		<high c=\"" . max ( $qw1, $qw2 ) . "\" f=\"" . (max ( $qw1, $qw2 ) * 1.8 + 32) . "\" />
		<low c=\"" . min ( $qw1, $qw2 ) . "\" f=\"" . (min ( $qw1, $qw2 ) * 1.8 + 32) . "\" />
		<winddir direction=\"{$wind}\"></winddir>
		<windspd kph=\"{$kph}\" mph=\"{$mph}\" mps=\"{$mps}\" />
		<uvi>0</uvi>
	</day>";
		
	/********************************************* 明天天气数据 *********************************************/
		// 处理日期数据
		$date = date("Y-m-d", dateadd("day", 1, strtotime($date))); 
		// 处理风向数据
		$fx = str_replace ( "风", "", $this->weather_data['tomorrow']["wind_direction_day"] );
		$wind = $this->windDirectionMap[$fx];
		// 处理风力数据 kph 每小时公里数 mph 每小时英里数
		$mps = str_replace ( array ( "小于", "级"), "", $this->weather_data['tomorrow']["wind_power_day"]);
		if ($mps == "微风") $mps = 1;
		if (mb_strpos ( $mps, "-" )) $mps = mb_substr ( $mps, mb_strpos ( $mps, "-" ) + 1 );
		if (! strlen ( $mps )) $mps = 0;
		$kph = isset($this->wcc_kph[$mps]) ? $this->wcc_kph[$mps] : "";
		$mph = intval ( $kph * 1.6093 );
		
		// 处理天气
		$tq1 = $this->weather_data['tomorrow']["weather_day"];
		$tq1 = str_replace("，", "转", $tq1);
		if(false!==strpos($tq1, "转")){
			$arr = explode("转", $tq1);
			$tq1 = $arr[0];
		}
		
		// 处理温度
		$qw1 = intval ( $this->weather_data ['tomorrow'] ["temperature_day"] );
		$qw2 = intval ( $this->weather_data ['tomorrow'] ["temperature_night"] );
		
		if (! $this->wcs_night_icons && (date ( "H" ) < $this->wcs_day_time_end || date ( "H" ) >= $this->wcs_day_time_start)) {
			//$tq1 = $this->weathercn_wcc_convert [$tq1]+32;
			$tq1 = $this->weathercn_wcc_convert [$tq1];
		} else {
			$tq1 = $this->weathercn_wcc_convert [$tq1];
		}
		
		$xml .= "
	<day number=\"1\">
		<name>" . date ( "D", strtotime ( $date ) ) . "</name>
		<date>" . date ( "n/j/Y", strtotime ( $date ) ) . "</date>
		<condition icon=\"" . $tq1 . "\">" . $this->wcc_weather[$tq1] . "</condition>
		<high c=\"" . max ( $qw1, $qw2 ) . "\" f=\"" . (max ( $qw1, $qw2 ) * 1.8 + 32) . "\" />
		<low c=\"" . min ( $qw1, $qw2 ) . "\" f=\"" . (min ( $qw1, $qw2 ) * 1.8 + 32) . "\" />
		<winddir direction=\"{$wind}\"></winddir>
		<windspd kph=\"{$kph}\" mph=\"{$mph}\" mps=\"{$mps}\" />
		<uvi>0</uvi>
	</day>";
		
		
	/********************************************* 后天天气数据 *********************************************/
		// 处理日期数据
		$date = date("Y-m-d", dateadd("day", 2, strtotime($date)));
		// 处理风向数据
		$fx = str_replace ( "风", "", $this->weather_data['after_tomorrow']["wind_direction_day"] );
		$wind = $this->windDirectionMap[$fx];
		// 处理风力数据 kph 每小时公里数 mph 每小时英里数
		$mps = str_replace ( array ( "小于", "级"), "", $this->weather_data['after_tomorrow']["wind_power_day"]);
		if ($mps == "微风") $mps = 1;
		if (mb_strpos ( $mps, "-" )) $mps = mb_substr ( $mps, mb_strpos ( $mps, "-" ) + 1 );
		if (! strlen ( $mps )) $mps = 0;
		$kph = isset($this->wcc_kph[$mps]) ? $this->wcc_kph[$mps] : "";
		$mph = intval ( $kph * 1.6093 );
		
		// 处理天气
		$tq1 = $this->weather_data['after_tomorrow']["weather_day"];
		if(false!==strpos($tq1, "转")){
			$arr = explode("转", $tq1);
			$tq1 = $arr[0];
		}
		
		// 处理温度
		$qw1 = intval ( $this->weather_data ['after_tomorrow'] ["temperature_day"] );
		$qw2 = intval ( $this->weather_data ['after_tomorrow'] ["temperature_night"] );
		
		if (! $this->wcs_night_icons && (date ( "H" ) < $this->wcs_day_time_end || date ( "H" ) >= $this->wcs_day_time_start)) {
			//$tq1 = $this->weathercn_wcc_convert [$tq1]+32;
			$tq1 = $this->weathercn_wcc_convert [$tq1];
		} else {
			$tq1 = $this->weathercn_wcc_convert [$tq1];
		}
		
		$xml .= "
	<day number=\"2\">
		<name>" . date ( "D", strtotime ( $date ) ) . "</name>
		<date>" . date ( "n/j/Y", strtotime ( $date ) ) . "</date>
		<condition icon=\"" . $tq1 . "\">" . $this->wcc_weather[$tq1] . "</condition>
		<high c=\"" . max ( $qw1, $qw2 ) . "\" f=\"" . (max ( $qw1, $qw2 ) * 1.8 + 32) . "\" />
		<low c=\"" . min ( $qw1, $qw2 ) . "\" f=\"" . (min ( $qw1, $qw2 ) * 1.8 + 32) . "\" />
		<winddir direction=\"{$wind}\"></winddir>
		<windspd kph=\"{$kph}\" mph=\"{$mph}\" mps=\"{$mps}\" />
		<uvi>0</uvi>
	</day>";
		
		
	/********************************************* 4-7天天气数据 *********************************************/
	for($i=0; $i<sizeof($this->weather_data["day_7"]); $i++){
		// 处理日期数据
		$date = date("Y-m-d", dateadd("day", 1, strtotime($date)));
		// 处理风向数据
		$fx = str_replace ( "风", "", $this->weather_data['day_7'][$i]["wind_direction_1"] );
		$wind = $this->windDirectionMap[$fx];
		// 处理风力数据 kph 每小时公里数 mph 每小时英里数
		$mps = str_replace ( array ( "小于", "级"), "", $this->weather_data['day_7'][$i]["wind_power"]);
		if ($mps == "微风") $mps = 1;
		if (mb_strpos ( $mps, "-" )) $mps = mb_substr ( $mps, mb_strpos ( $mps, "-" ) + 1 );
		if (! strlen ( $mps )) $mps = 0;
		$kph = isset($this->wcc_kph[$mps]) ? $this->wcc_kph[$mps] : "";
		$mph = intval ( $kph * 1.6093 );
		
		// 处理天气
		$tq1 = $this->weather_data['day_7'][$i]["weather"];
		$tq1 = str_replace("，", "转", $tq1);
		if(false!==strpos($tq1, "转")){
			$arr = explode("转", $tq1);
			$tq1 = $arr[0];
		}
		
		// 处理温度
		$qw1 = intval ( $this->weather_data ['day_7'][$i] ["temperature_1"] );
		$qw2 = intval ( $this->weather_data ['day_7'][$i] ["temperature_2"] );
		
		if (! $this->wcs_night_icons && (date ( "H" ) < $this->wcs_day_time_end || date ( "H" ) >= $this->wcs_day_time_start)) {
			//$tq1 = $this->weathercn_wcc_convert [$tq1]+32;
			$tq1 = $this->weathercn_wcc_convert [$tq1];
		} else {
			$tq1 = $this->weathercn_wcc_convert [$tq1];
		}
		
		$xml .= "
	<day number=\"" . (3+$i) . "\">
		<name>" . date ( "D", strtotime ( $date ) ) . "</name>
		<date>" . date ( "n/j/Y", strtotime ( $date ) ) . "</date>
		<condition icon=\"" . $tq1 . "\">" . $this->wcc_weather[$tq1] . "</condition>
		<high c=\"" . max ( $qw1, $qw2 ) . "\" f=\"" . (max ( $qw1, $qw2 ) * 1.8 + 32) . "\" />
		<low c=\"" . min ( $qw1, $qw2 ) . "\" f=\"" . (min ( $qw1, $qw2 ) * 1.8 + 32) . "\" />
		<winddir direction=\"{$wind}\"></winddir>
		<windspd kph=\"{$kph}\" mph=\"{$mph}\" mps=\"{$mps}\" />
		<uvi>0</uvi>
	</day>";
	}
		
		
		
		
		
		$xml .= "
</forecast>
<copyright>阿枫,http://w.mdeve.com uptime:". date("Y-m-d H:i:s")."</copyright>
<usage>Manila 3D Weather www.weather.com.cn</usage>
</weather>";
		
		header("Content-Type: text/xml; charset=utf-8");
		echo $xml;
		
	}
	
	
}